#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <Judy.h>
#include <assert.h>
#include "lib.h"

int load_dict()
{
	char *dict_file="dict.txt";
	PWord_t v;
	int i=0;
	FILE *fp=fopen(dict_file,"r");
	if(!fp)
	{
		perror("fopen");
		exit(1);
	}
	char line[1024]={0};
	while(!feof(fp))
	{
		memset(line,0,sizeof(line));
		fgets(line,1024,fp);
		line[strlen(line)-1]='\0';
		if(line[0]== 0 )continue;
		JHSI(v,ja,line,strlen(line));
		if(*v == 0) *v=1;
		i++;
	}
	fclose(fp);
	return i;
}
unsigned char *gbk_split(unsigned char *s,unsigned int reverse)
{
	unsigned char *o=calloc(1,2*strlen(s));
	unsigned char *rsw;
	unsigned char *ret=o;
	unsigned char *t;
	int i=0,j=0,k=0;
	int last=0;
	PWord_t v;
	unsigned char b[1024]={0};
	while(*s)
	{
	if(*s < 128)
	{
		// if a string is english ,leave it alone
		if(*(o-1) != '/')
		{
			*o='/';
			o++;
		}
		while(*s < 128)
		{
			*o=*s;
			o++;
			s++;
		}
		*o='/';
		o++;
		t=s;
		k=0;
	}
	else
	{
		t=s;
		k=0;
		while(*s >= 128)
		{
			//if stop_word, make is as a word,break.
			if( is_chinese_stop_word((unsigned short *)s)) 
			{
				if( t == s)
				{
					s+=2;
					k+=2;
					t+=2;
					if(*(o-1) != '/')
					{
						*o='/';
						o++;
					}
					memcpy(o,s-2,2);
					//printf("ccccccccc %s\n",o);
					o+=2;
					*o='/';
					o++;
				}
				break;

			}
			s+=2;
			k+=2;
			if(k >= 16) break;
		}
		int kk=k;
		int sn=0;
		int sm=0;
		while(t < s)
		{
		//FMM
		for(i=k;i>=0;i-=2)
		{
			memset(b,0,sizeof(b));
			memcpy(b,t,i);
			//printf("%s\n",b);
			if(reverse == 1)
			{
				rsw=reverse_str((unsigned char *)b);
				JHSG(v,ja,rsw,i);
				free(rsw);
			}
			else
			{
				JHSG(v,ja,t,i);
			}
			if(v == NULL)
			{
				if(i == 0 )
				{
					memcpy(o,t,2);
					o+=2;
					t+=2;
					k-=2;
					sn++;
				}
				continue;
			}
			else
			{
				if(*(o-1) != '/')
				{
					*o='/';
					o++;
				}
				memcpy(o,t,i);
				o+=i;
				t+=i;
				k-=i;

				*o='/';
				o++;
				
				sm++;
				sn=0;
				break;
			}
		}
		}
		if(( kk >= 4 && sm == 0 ) ||  (kk > 2 && sn > 0))
		{
			s-=2;
			o-=2;
		}

	}
	}
	return ret;

}
int load_chinese_stop_word()
{
	stop_word=calloc(200,sizeof(unsigned short));
	int i=0;
	FILE *fp=fopen("filter.txt","r");
	if(!fp)
	{
		perror("fopen");
		return -1;
	}
	unsigned char line[16];
	while(!feof(fp))
	{
		fgets(line,16,fp);
		line[strlen(line)-1]='\0';
		if(strlen(line) != 2) continue;
		memcpy((void *)(stop_word+i),line,2);
		i++;
	}
	stop_word_size=i+1;
	return i;
}

int is_chinese_stop_word(unsigned short *s)
{
	int i=0;
	for(i=0;i<stop_word_size;i++)
	{
		if(*s == *(stop_word+i)) return 1;
	}
	return 0;
}

unsigned char *reverse_str(unsigned char *s)
{
	int c=strlen(s);
	unsigned char *o=calloc(1,c+1);
	assert(o != NULL);
	unsigned char *ret=o+c-1;
	while(*s)
	{
		if(*s > 128)
		{
			*(ret-1)=*s;
			*(ret)=*(s+1);
			ret-=2;
			s+=2;
		}
		else
		{
			*ret=*s;
			ret--;
			s++;
		}

	}
	return o;
}
